package net.cyan.proteus.compile;

import net.cyan.commons.util.StringUtils;
import net.cyan.proteus.StringElement;

/**
 * 
 * @author camel
 * @date 2008-4-9
 */
public class StringCompiler implements ElementCompiler<StringElement>
{
    public StringCompiler()
    {
    }

    public void compile(StringElement element, CompileContext context) throws Exception
    {
        String[] ss = element.getBuffer().toString().split("\n");

        StringBuilder buffer = context.getNormal();
        boolean b = false;
        for (String s : ss)
        {
            if (b)
                buffer.append("writer.write(\"\\n\");\r\n");

            int length = s.length();
            int pos = 0;
            while (pos < length)
            {
                int pos1 = pos + 128;
                if (pos1 > length)
                    pos1 = length;

                buffer.append("writer.write(\"");
                buffer.append(StringUtils.escapeString(s.substring(pos, pos1)));
                buffer.append("\");\r\n");

                pos = pos1;
            }

            b = true;
        }
    }
}
